package pannelli.main;
import it.jack.Main;
import it.jack.PannelloPrincipale;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.util.ArrayList;


import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import database.DB;

import javax.print.attribute.standard.JobHoldUntil;
import javax.swing.*;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import javax.swing.table.DefaultTableModel;

import oggetti.Cliente;
import pannelli.AggiungiCliente;
import pannelli.sub.SubPannelloVisualizzaClienti;


/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class PannelloClienti extends JInternalFrame implements InternalFrameListener{

	private static final long serialVersionUID = 5774832809090882649L;
	private JButton RicercaCliente;
	private JButton aggiungi;
	private DefaultTableModel model;
	private static PannelloClienti istance;

	public PannelloClienti() throws PropertyVetoException{
		super("Pannello Clienti",true,true,true,true);
		initGUI();
		this.istance=this;
		buttonAction();
		setSelected(true);
		setPreferredSize(new Dimension(300,150));
		this.addInternalFrameListener(this);
	}
	public void disableButtons(){
		RicercaCliente.setEnabled(false);
		aggiungi.setEnabled(false);
	}
	public void activeButtons(){
		RicercaCliente.setEnabled(true);
		aggiungi.setEnabled(true);
	}
	public static PannelloClienti getIstance(){
		return istance;
	}
	public int getCurrentRow(){
		return SubPannelloVisualizzaClienti.getInstance().getCurrentRow();
	}
	public int getMaxRow(){
		return SubPannelloVisualizzaClienti.getInstance().getMaxRow();
	}
	public Cliente getClienteFromRow(int row){
		return SubPannelloVisualizzaClienti.getInstance().getClienteFromRow(row);
	}
	public void updateTable(){
		SubPannelloVisualizzaClienti.getInstance().initTable();
	}
	private void buttonAction(){
		aggiungi.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				AggiungiCliente a = new AggiungiCliente();
				a.setVisible(true);
				a.setEnabled(true);
				Main.getInstance().addInDesktop(a);
				try {
					setSelected(false);
				} catch (PropertyVetoException e) {
					e.printStackTrace();
				}
			}
		});
		RicercaCliente.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Main.getInstance().addInDesktop(new PannelloRicerca(0));
			}
		});
	}
	public void updateTable(Cliente c){
		int a = model.getRowCount();
		for(int i=0;i<a;i++)
			model.removeRow(0);

		model.addRow(c.toArray());
	}

	public void centra(){
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(new Point((dimension.width - getSize().width) / 2, 
				(dimension.height - getSize().height) / 2 ));
	}
	private void initGUI(){
		{
			AnchorLayout thisLayout = new AnchorLayout();
			this.setLayout(thisLayout);
			//this.setPreferredSize(new java.awt.Dimension(800, 700));
			{
                                SubPannelloVisualizzaClienti.getInstance().svuotaTabella();
                                SubPannelloVisualizzaClienti.getInstance().initTable();
				getContentPane().add(SubPannelloVisualizzaClienti.getInstance(), new AnchorConstraint(9, 729, 1001, 3, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				SubPannelloVisualizzaClienti.getInstance().setPreferredSize(new java.awt.Dimension(490, 369));
			}
			this.setSize(600, 300);
			{
				aggiungi = new JButton();
				getContentPane().add(aggiungi, new AnchorConstraint(63, 963, 146, 774, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				aggiungi.setText("Aggiungi Cliente");
				aggiungi.setPreferredSize(new java.awt.Dimension(128, 32));
			}
			{
				RicercaCliente = new JButton();
				getContentPane().add(RicercaCliente, new AnchorConstraint(208, 963, 286, 774, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				RicercaCliente.setText("Ricerca Cliente");
				RicercaCliente.setPreferredSize(new java.awt.Dimension(128, 32));
			}
			centra();
		}
	}
	@Override
	public void internalFrameActivated(InternalFrameEvent arg0) {
		PannelloPrincipale.getIstance().disableButtons();
	}
	@Override
	public void internalFrameClosed(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void internalFrameClosing(InternalFrameEvent arg0) {
		PannelloPrincipale.getIstance().activateButtons();
		this.dispose();
	}
	@Override
	public void internalFrameDeactivated(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void internalFrameDeiconified(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void internalFrameIconified(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void internalFrameOpened(InternalFrameEvent arg0) {
		PannelloPrincipale.getIstance().disableButtons();
	}
}
